var mysql = require('mysql2');
const {sqlConfig} = require('./systemConfig');
const utils = require('../utils/utils');

const writeSQL = (sql, params) => {
    return new Promise(async (resolve, reject) => {
        const connection = await mysql.createConnection(sqlConfig);
        // const connection = mysql.createPool(sqlConfig);
        await connection.connect();
        connection.query(sql, params, (err, result) => {
            connection.end();
            if (err) {
                reject(err);
            } else {
                if (result && result.length > 0) {
                    result = result.map(obj => utils.formatObjectKeys(obj));
                }
                resolve(result);
            }
        });
        
    });
};


module.exports = writeSQL;